package ar.com.ctrln.view.dashboard.principal;

import java.util.List;

import org.zkoss.spring.security.SecurityUtil;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.select.annotation.VariableResolver;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zk.ui.select.annotation.WireVariable;
import org.zkoss.zul.Label;
import org.zkoss.zul.ListModel;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

import ar.com.ctrln.control.principal.ConsultarAlerta;

import ar.com.ctrln.persistence.model.sistema.dashboardPpal.Alerta;

import ar.com.ctrln.view.ui.comportamientoVentana.AbstractMinimizarVentana;

@VariableResolver(org.zkoss.zkplus.spring.DelegatingVariableResolver.class)
public class AlertaController extends AbstractMinimizarVentana {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@WireVariable
	private ConsultarAlerta consultarAlerta;

	private List<Alerta> alerta;

	@Wire
	private Window wAlertas;

	@Wire
	private Listbox lbAlerta;

	@Wire("#txtFilter")
	private Textbox txtFilter;

	@Wire
	private Label footer_label;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);

		if (!SecurityUtil.isAllGranted("ROLE_ADMIN")) {
			wAlertas.setVisible(false);
		} else {
			alerta = consultarAlerta.listaAlerta();
			ListModelList<Alerta> modelo = new ListModelList<Alerta>();
			modelo.addAll(alerta);
			lbAlerta.setModel(modelo);
		}

	}

	public ListModel<Alerta> getConsultaAlerta() {
		alerta = consultarAlerta.listaAlerta();
		return new ListModelList<Alerta>(alerta);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void getFilter(Event event) {
		((Textbox) event.getTarget())
				.setValue(((org.zkoss.zk.ui.event.InputEvent) event).getValue());

		lbAlerta.setModel(new ListModelList(alerta));

		if (lbAlerta.getItemCount() > 0) {
			footer_label.setValue("");
			footer_label.setValue("Un total de " + lbAlerta.getItemCount()
					+ " Clientes");
		} else {
			footer_label.setValue("Ningun Resultado...");
			footer_label.setValue("");
		}
		((Textbox) event.getTarget()).setFocus(true);
	}

}
